package com.gurun.camera.device.presenter.impl;

import android.os.Handler;
import android.os.Looper;

import com.danale.player.SPlayer;
import com.danale.player.content.DAudioRecord;
import com.danale.player.entity.CloudRecordDevice;
import com.danale.player.entity.MultiChannelDevice;
import com.danale.player.entity.SdRecordDevice;
import com.danale.player.listener.MediaState;
import com.danale.player.window.ScreenType;
import com.gurun.camera.device.constant.VideoDataType;
import com.gurun.camera.device.listener.OnMediaStateListener;
import com.gurun.camera.device.listener.OnPlayerClickListener;
import com.gurun.camera.device.model.ControlManager;
import com.gurun.camera.device.model.impl.VideoControlManager;
import com.gurun.camera.device.presenter.IVideoPresenter;
import com.gurun.camera.device.view.ILivePlayView;
import com.gurun.camera.device.view.IPlayerGestureView;
import com.gurun.camera.mvp.IBaseView;

public class VideoPresenter implements IVideoPresenter, OnMediaStateListener, OnPlayerClickListener {

    private ControlManager controlManager;
    private ILivePlayView videoPlayView;
    private IPlayerGestureView playerGestureView;

    public VideoPresenter(IBaseView baseView, VideoDataType type, SPlayer sPlayer, boolean isCardPlay){
        if (baseView instanceof ILivePlayView){
            videoPlayView = (ILivePlayView) baseView;
        }
        if (baseView instanceof IPlayerGestureView){
            playerGestureView = (IPlayerGestureView) baseView;
        }
        controlManager = new VideoControlManager(type, sPlayer, isCardPlay);

        controlManager.setOnMediaStateListener(this);
        controlManager.setOnPlayerClickListener(this);
    }

    /**
     * @param playView //
     * @param type //
     * @param sPlayer //Player
     */
    public VideoPresenter(ILivePlayView playView, VideoDataType type, SPlayer sPlayer){
        this(playView, type, sPlayer, false);
    }

    @Override
    public void initPlay(int width, float factor, ScreenType screenType) {
        controlManager.initPlay(width, factor, screenType);
    }

    @Override
    public void release() {
        controlManager.release();
    }

    @Override
    public void prepare() {
        controlManager.prepare();
    }

    @Override
    public void startVideo() {
        controlManager.startVideo();
    }

    @Override
    public void clickAudio() {
        new Thread(){
            @Override
            public void run() {
                super.run();
                controlManager.clickAudio();
            }
        }.start();
    }

    @Override
    public void startAudio() {
        controlManager.startAudio();
    }

    @Override
    public void stopAudio() {
        controlManager.stopAudio();
    }

    @Override
    public void startTalk() {
        controlManager.startTalk();
    }

    @Override
    public void stopTalk() {
        controlManager.stopTalk();
    }

    @Override
    public void setIsSilence(boolean isSilence) {
        controlManager.setIsSilence(isSilence);
    }

    @Override
    public void clickTalk() {
        controlManager.clickTalk();
    }

    @Override
    public void clickRecord() {
        controlManager.clickRecord();
    }

    @Override
    public void stopVideo() {
        controlManager.stopVideo();
    }

    @Override
    public void stopVideo(boolean closeConnection) {
        controlManager.stopVideo(closeConnection);
    }

    @Override
    public void stopVideoData() {
        controlManager.stopVideoData();
    }

    @Override
    public void capture() {
        controlManager.capture();
    }

    @Override
    public void stopRecord() {
        controlManager.stopRecord();
    }

    @Override
    public void selectSubScreen(String device_id) {
        controlManager.selectSubScreen(device_id);
    }

    @Override
    public void startVideo(boolean visible) {
        controlManager.startVideo(visible);
    }

    @Override
    public void pause() {
        controlManager.pause();
    }

    @Override
    public void resume() {
        controlManager.resume();
    }

    @Override
    public int getCurrentPosition() {
        return controlManager.getCurrentPosition();
    }

    @Override
    public int getDuration() {
        return controlManager.getDuration();
    }

    @Override
    public void seekTo(int msic) {
        controlManager.seekTo(msic);
    }

    @Override
    public void setChannel(int channel) {
        controlManager.setChannel(channel);
    }

    @Override
    public void changeChannels(int[] channel) {
        controlManager.changeChannel(channel);
    }

    @Override
    public void setData(String id) {
        controlManager.setId(id);
    }

    @Override
    public void updateData(String id) {
        controlManager.updateData(id);
    }

    @Override
    public void setCloudPlayData(CloudRecordDevice device) {
        controlManager.setCloudRecordData(device);
    }

    @Override
    public void setSDRecordData(SdRecordDevice device) {
        controlManager.setSDRecordData(device);
    }

    @Override
    public void setLocalRecordData(String path) {
        controlManager.setLocalRecordData(path);
    }

    @Override
    public void setMultiChanData(MultiChannelDevice device) {
        controlManager.setMultiChanDevice(device);
    }

    @Override
    public void multiply() {
        controlManager.switchScreenState();
    }

    @Override
    public void setRecordCallback(DAudioRecord.RecordCallback callback) {
        controlManager.setRecordCallback(callback);
    }

    @Override
    public void onVideoStateChange(final String id, final MediaState state) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                if (videoPlayView != null){
                    videoPlayView.showVideoState(id, state);
                }
            }
        });
    }

    @Override
    public void onAudioStateChange(final MediaState state) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                if (videoPlayView != null){
                    videoPlayView.showAudioState(state);
                }
            }
        });
    }

    @Override
    public void onTalkStateChange(final MediaState state) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                if (videoPlayView != null){
                    videoPlayView.showTalkState(state);
                }
            }
        });
    }

    @Override
    public void onRecordStateChange(final MediaState state, final int time, final String filepath) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                if (videoPlayView != null){
                    videoPlayView.showRecordState(state, switchTime(time), filepath);
                }
            }
        });
    }

    @Override
    public void onCaptureStateChange(final MediaState state, final String path) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                if (videoPlayView != null){
                    videoPlayView.showCaptureState(state, path);
                }
            }
        });
    }

    @Override
    public void onSingleClick(final String id) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                if (playerGestureView != null){
                    playerGestureView.onSingleClick(id);
                }
            }
        });
    }

    @Override
    public void onDoubleClick() {
        if (playerGestureView != null){
            playerGestureView.onDoubleClick();
        }
    }

    /**
     *
     * @param time
     * @return
     */
    public static String switchTime(int time){
        int i = time % 60;
        int j = time / 60;
        int k = j / 60;
        String ks = k >= 10 ? "" + k : "0" + k;
        String js = j >= 10 ? "" + j : "0" + j;
        String is = i >= 10 ? "" + i : "0" + i;
        return ks + ":" + js + ":" + is;
    }

    /**
     *
     * @param time
     * @return
     */
    public static String switchRecordTime(int time){
        int i = time / 1000;
        int j = i / 60;
        int k = j / 60;
        i = i % 60;
        j = j % 60;
        k = k % 24;
        String ks = k >= 10 ? "" + k : "0" + k;
        String js = j >= 10 ? "" + j : "0" + j;
        String is = i >= 10 ? "" + i : "0" + i;
        if (k > 0){
            return ks + ":" + js + ":" + is;
        }else {
            return js + ":" + is;
        }
    }

    public static String formatTimerTime(int time) {
        int hour = time / 3600;
        int min = (time % 3600) / 60;
        int sec = (((time % 3600) % 60));
        StringBuilder strBuilder = new StringBuilder();
        if (hour < 10) {
            strBuilder.append("0");
        }
        strBuilder.append(hour);
        strBuilder.append(":");
        if (min < 10) {
            strBuilder.append(0);
        }
        strBuilder.append(min);
        strBuilder.append(":");
        if (sec < 10) {
            strBuilder.append(0);
        }
        strBuilder.append(sec);
        return strBuilder.toString();
    }
}
